# progressive_viewer.sc - a document viewer for pdf2swf converted files
#
# Copyright (c) 2010 Ricardo Pedroso 
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following condition:
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

.flash filename=progressive_viewer.swf version=8 background=#666666

    .png first_idle first.png
    .png first_hover first.png
    .png first_pressed first.png

    .png prev_idle prev.png
    .png prev_hover prev.png
    .png prev_pressed prev.png

    .png next_idle next.png
    .png next_hover next.png
    .png next_pressed next.png

    .png last_idle last.png
    .png last_hover last.png
    .png last_pressed last.png

    .png zoom_out_idle zoom-out.png
    .png zoom_out_hover zoom-out.png
    .png zoom_out_pressed zoom-out.png

    .png zoom_in_idle zoom-in.png
    .png zoom_in_hover zoom-in.png
    .png zoom_in_pressed zoom-in.png

    .png zoom_100_idle zoom-100.png
    .png zoom_100_hover zoom-100.png
    .png zoom_100_pressed zoom-100.png

    .png zoom_best_idle zoom-best.png
    .png zoom_best_hover zoom-best.png
    .png zoom_best_pressed zoom-best.png

    .png zoom_width_idle zoom-width.png
    .png zoom_width_hover zoom-width.png
    .png zoom_width_pressed zoom-width.png

    .png fullscreen_idle fullscreen.png
    .png fullscreen_hover fullscreen.png
    .png fullscreen_pressed fullscreen.png

    .png about_idle about.png
    .png about_hover about.png
    .png about_pressed about.png

    .dropshadow myshadow color=#333333 blur=15 angle=90 distance=2 passes=2 strength=1
    #.dropshadow myshadow2 color=#cccccc blur=5 angle=0 distance=1 passes=2 strength=1

    #.font arialbold filename="../wx/ArialBold.ttf"
    #.text tloading text="Loading..." arialbold size=24pt color=#333333
    #.box area 100 20

    .sprite amount_outline
        .box bamount_outline 101 22 color=#333333 fill=gray
        .put bamount_outline pin=center x=0
    .end

    .sprite amount
        .box bamount 100 20 color=red fill=red
        .put bamount pin=center x=0
    .end
    #.edittext amount width=70 height=45 font=arialbold color=#333333 noselect size=24pt align=right

    .sprite loading
        .put amount_outline
        .put amount
    .end


    .button first
        .show first_idle as=idle
        .show first_idle as=area
        .show first_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show first_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            gotoFirstPage();
        .end
    .end

    .button prev
        .show prev_idle as=idle
        .show prev_idle as=area
        .show prev_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show prev_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            gotoPrevPage();
        .end
    .end

    .button next
        .show next_idle as=idle
        .show next_idle as=area
        .show next_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show next_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            gotoNextPage();
        .end
    .end

    .button last
        .show last_idle as=idle
        .show last_idle as=area
        .show last_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show last_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            gotoLastPage();
        .end
    .end

    .button zoom_in
        .show zoom_in_idle as=idle
        .show zoom_in_idle as=area
        .show zoom_in_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show zoom_in_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            zoom = zoom + 10;
            setZoom(zoom);
        .end
    .end

    .button zoom_out
        .show zoom_out_idle as=idle
        .show zoom_out_idle as=area
        .show zoom_out_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show zoom_out_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            zoom = zoom - 10;
            setZoom(zoom);
            if (Stage.height - 55 > viewport._height) {
                viewport._y = 55;
            } else if (Stage.height > viewport._height + viewport._y) {
                viewport._y = Stage.height - viewport._height;
            }
        .end
    .end

    .button zoom_100
        .show zoom_100_idle as=idle
        .show zoom_100_idle as=area
        .show zoom_100_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show zoom_100_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            zoom = 100;
            setZoom(zoom);
            if (Stage.height - 55 > viewport._height) {
                viewport._y = 55;
            } else if (Stage.height > viewport._height + viewport._y) {
                viewport._y = Stage.height - viewport._height;
            }
        .end
    .end

    .button zoom_best
        .show zoom_best_idle as=idle
        .show zoom_best_idle as=area
        .show zoom_best_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show zoom_best_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            bestx = viewport._width / viewport._xscale * 100;
            besty = viewport._height / viewport._yscale * 100;
            scalex = Stage.width / bestx * 100;
            scaley = (Stage.height-55) / besty * 100;
            if (scalex < scaley) {
                zoom = scalex;
                viewport._x = 0;
            } else {
                zoom = scaley;
                viewport._y = 55;
            }
            setZoom(zoom);
        .end
    .end

    .button zoom_width
        .show zoom_width_idle as=idle
        .show zoom_width_idle as=area
        .show zoom_width_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show zoom_width_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            bestx = viewport._width / viewport._xscale * 100;
            scalex = Stage.width / bestx * 100;
            zoom = scalex;
            setZoom(zoom);
        .end
    .end

    .button fullscreen
        .show fullscreen_idle as=idle
        .show fullscreen_idle as=area
        .show fullscreen_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show fullscreen_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            if (Stage.displayState == "fullScreen") {
                Stage.displayState = "normal";
            } else {
                Stage.displayState = "fullScreen";
            }
            viewport._y = 55;
        .end
    .end

    .button about
        .show about_idle as=idle
        .show about_idle as=area
        .show about_hover as=hover scale=102% red=+30 blue=+30 green=+30
        .show about_pressed as=pressed scale=99% red=-30 blue=-30 green=-30
        .on_release:
            showAbout();
        .end
    .end

    .sprite toolbar
        #.box btoolbar width=2000 height=55 color=gray fill=none line=0
        #.put btoolbar
        .action:
            height = 55;
            width = Stage.width;
            rect = this.createEmptyMovieClip("", this.getNextHighestDepth());
            rect.clear();
            //rect.beginFill(0xefefef);
            //thickness, rgb, alpha, pixelHinting, noScale, capsStyle, jointStyle, miterLimit
            rect.lineStyle(0, 0x000000, 0, true, "none", "none");
            rect.moveTo(0,0);
            rect.lineTo(0,height);
            rect.lineTo(width,height);
            rect.lineTo(width,0);
            rect.endFill();
        .end
    .end

    .sprite about_viewport
    .end

    .sprite viewport
    .end

    .sprite pre_viewport
    .end

    .put loading pin=top-left x=100 y=100

    .put pre_viewport pin=top-left x=0 y=54 alpha=0%
    .put viewport pin=top-left x=0 y=55

    .put toolbar pin=top-left x=0 y=0 filter=myshadow

    .put first pin=top-left x=0 y=0 scale=99%
    .put prev pin=top-left x=54 y=0 scale=99%
    .put next pin=top-left x=108 y=0 scale=99%
    .put last pin=top-left x=162 y=0 scale=99%
    .put zoom_out pin=top-left x=216 y=0 scale=99%
    .put zoom_in pin=top-left x=270 y=0 scale=99%
    .put zoom_100 pin=top-left x=324 y=0 scale=99%
    .put zoom_best pin=top-left x=378 y=0 scale=99%
    .put zoom_width pin=top-left x=432 y=0 scale=99%
    .put fullscreen pin=top-left x=486 y=0 scale=99%
    .put about pin=top-left x=540 y=0 scale=99%

    .put about_viewport pin=top-left x=100 y=100

.frame 1
    .action:
        onMouseDown = function(){
            if (_ymouse > 54) {
                about_viewport._visible = false;
                left = Stage.width-viewport._width;
                top = Stage.height-viewport._height;
                if ( (left < 0 || viewport._x < 0) && (top < 0 || viewport._y < 55) ) {
                    viewport.startDrag(0, left, top, 0, 55);
                } else if (left < 0 || viewport._x < 0) {
                    viewport.startDrag(0, left, 55, 0, 55);
                } else if (top < 0 || viewport._y < 55) {
                    viewport.startDrag(0, viewport._x, top, viewport._x, 55);
                }
            }
        };
        
        //onMouseMove = function(){
        //};
        
        onMouseUp = function(){
            viewport.stopDrag();
        };

        setZoom = function(zoom) {
            pre_viewport._xscale = 0;
            pre_viewport._yscale = 0;
            viewport._xscale = zoom;
            viewport._yscale = zoom;
            viewport.opaqueBackground = 0x333333;
            viewport._x = Stage.width / 2 - viewport._width / 2;
        };
    
        gotoNextPage = function() {
            unLoadPage(pagenr);
            pagenr = pagenr + 1;
            if (pagenr >= pagecnt) {
                pagenr = pagecnt;
                loadPage(pagenr, 0);
            } else {
                loadPage(pagenr, 1);
            }
        };

        gotoPrevPage = function() {
            unLoadPage(pagenr);
            pagenr = pagenr - 1;
            if (pagenr <= 1) {
                pagenr = 1;
                loadPage(pagenr, 0);
            } else {
                loadPage(pagenr, -1);
            }
        };

        gotoFirstPage = function() {
            unLoadPage(pagenr);
            pagenr = 1;
            loadPage(1, 1);
        };

        gotoLastPage = function() {
            unLoadPage(pagenr);
            pagenr = pagecnt;
            loadPage(pagecnt, -1);
        };
    
        unLoadPage = function(pagenr) {
            loader.unLoadClip(pageprefix + pagenr + ".swf");
        };

        loadPage = function(pagenr, preload) {
            loader.loadClip(pageprefix + pagenr + ".swf", viewport);
            if (preload) {
                loadMovie(pageprefix + int(pagenr+preload) + ".swf", pre_viewport);
            }
        };

        showAbout = function() {
            about_viewport._x = Stage.width / 2 - about_viewport._width / 2;
            about_viewport._y = Stage.height / 2 - about_viewport._height / 2;
            about_viewport._visible = !about_viewport._visible;
        };

        function onLoadInit(_mc) {
            loading._visible = false;
            viewport.opaqueBackground = 0x333333;
            viewport._x = Stage.width / 2 - viewport._width / 2;
            viewport._y = 55;
        };

        function onLoadStart(_mc) {
            loading.amount._width = 0;
            loading._visible = true;
        };

        function onLoadProgress(_mc, loaded, total) {
            loading.amount._width = int(loaded * 100 / total);
        };

        listener = new Object();
        listener.onLoadInit = function(_mc) {
            about_viewport._visible = false;
        };

        loader = new MovieClipLoader();
        loader.addListener(this);

        about_loader = new MovieClipLoader();
        about_loader.addListener(listener);

        pagenr = int(page);
        pagecnt = int(pagecnt);

        zoom = int(zoom);
        if (!zoom) {
            zoom = 100;
        }

        if (!pageprefix) {
            pageprefix = "page";
        }

        if (!tbbgcolor) {
            tbbgcolor = 0xdedede;
        } else {
            tbbgcolor = int(tbbgcolor);
        }

        Stage.scaleMode = "noScale";
        Stage.align = "LT";

        about_viewport._visible = false;
        loading._visible = false;

        setZoom(zoom);

        gotoFirstPage();

        toolbar._width = Stage.width;
        toolbar.opaqueBackground = tbbgcolor;

        if (!aboutpage) {
            about._visible=false;
        } else {
            about_loader.loadClip(aboutpage, about_viewport);
        }

        reposition();

        var mouseListener = new Object();

        mouseListener.onMouseWheel = function(delta) {
            divideDelta = delta/Math.abs(delta);
            viewport._y = viewport._y + 20 * divideDelta;
            if (viewport._y > 55) {
                viewport._y = 55;
            } else if (Stage.height > viewport._height + viewport._y) {
                viewport._y = Stage.height - viewport._height;
            }
        };

        Mouse.addListener(mouseListener);

        stage_listener = new Object();
        stage_listener.onResize = function()
        {
            reposition();
        };
        Stage.addListener(stage_listener);

        function reposition() {
            toolbar._width = Stage.width;

            viewport._x = Stage.width / 2 - viewport._width / 2;

            about_viewport._x = Stage.width / 2 - about_viewport._width / 2;
            about_viewport._y = Stage.height / 2 - about_viewport._height / 2;

            loading._x = Stage.width / 2;
            loading._y = Stage.height / 2;
        };

    .end

.end
